package com.example.orderservice.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.orderservice.client.ProductServiceFeignClient;
import com.example.orderservice.entity.Order;
import com.example.orderservice.entity.PageVO;
import com.example.orderservice.entity.Product;
import com.example.orderservice.service.OrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author lenovo
 * @Description
 * @Date2025/4/14 下午4:38
 */
//@Slf4j
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;


    @RequestMapping("/order/{id}")
    @SentinelResource(value = "order/{id}",blockHandler = "findOrderByIdExcpetion",blockHandlerClass = ExceptionHandler.class)
    public ResponseEntity<Order> findOrderById(@PathVariable Integer id) {
        Order order = orderService.getById(id);
        return ResponseEntity.ok(order);
    }


//    public ResponseEntity<Order> findProductById(@PathVariable Integer id) {
//        Order order = orderService.getById(id);
//        //使用feign客户端
//        ResponseEntity<Product> entity = productServiceFeignClient.findProductById(id);
//        order.setProduct(entity.getBody());
//        return ResponseEntity.ok(order);
//    }


    @GetMapping("/orders")
    public ResponseEntity<List<Order>> findAllOrders() {
        List<Order> list =orderService.list();
        return ResponseEntity.ok(list);
    }


    @GetMapping("order-page")
    public ResponseEntity<PageVO> findOrdersPage(@RequestParam(value = "current",required = false,defaultValue = "1") Integer current,
                                                   @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pagesize){
        Page<Order> page=orderService.page(new Page<>(current,pagesize));
        PageVO pageVO=new PageVO(current,pagesize,page.getTotal(),page.getRecords());
        return ResponseEntity.ok(pageVO);
    }

    @PostMapping("/order")
    public ResponseEntity<String> addOrder(@RequestBody Order order) {
        orderService.save(order);
        return ResponseEntity.ok("ok");
    }

    @PutMapping("/order")
    public ResponseEntity<String> updateOrder(@RequestBody Order order) {
        orderService.updateById(order);
        return ResponseEntity.ok("ok");
    }

    @DeleteMapping("/order/{id}")
    public ResponseEntity<String> deleteOrder(@PathVariable Integer id) {
        orderService.removeById(id);
        return ResponseEntity.ok("ok");
    }




        //通过网络调用商品的查询服务
//        ResponseEntity<Product> entity=restTemplate.getForEntity("http://127.0.0.1:8001/product/" +order.getProductId(),
//                Product.class);

        //使用服务名+注册中心调用服务
//        ResponseEntity<Product> entity=
//                restTemplate.getForEntity("http://product-service/product/" +order.getProductId(),
//                Product.class);



//    @GetMapping("/order")
//    public String order(){
//        log.info("Thread:{},time:{}",Thread.currentThread().getName(),System.currentTimeMillis());
//        return "这是订单接口!";
//    }
//
//    @GetMapping("/payment")
//    public String payment(){
//        return "这是支付接口!";
//    }
}
